﻿#region
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharpObservation.Tests.Dummy;
using Desc = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
#endregion

namespace SharpObservation.Tests
{
    /// <summary>
    ///   Summary description for DelegateTests
    /// </summary>
    [TestClass]
    public class DiposableTests
    {
        public class MyDisposable : DisposableObject
        {
            public MyDisposable Child;
            public MyDisposable[] Children;

            public MyDisposable()
            {
            }

            public void TestThrowIfDisposed()
            {
                ThrowIfDisposed();
            }
        }


        ///<summary>
        ///  Gets or sets the test context which provides
        ///  information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //

        #endregion

        private MyDisposable DisposableDumy
        {
            get
            {
                var child = new MyDisposable();
                var children = new[] { new MyDisposable(), new MyDisposable() };
                return new MyDisposable()
                {
                    Child = child,
                    Children = children.ToArray()  
                };
            }
        }

        [TestMethod]
        [Desc("Confirms that dynamically generated disposer does not raise exceptions")]
        public void TestDisposableObject()
        {
            using (var d1 = new MyDisposable())
            { }
        }

        [TestMethod]
        [Desc("Confirms the ThrowIfDisposed works correctly")]
        [ExpectedException(typeof(ObjectDisposedException), "ThrowIfDisposed works correctly")]
        public void TestThrowIfDisposed()
        {
            var d1 = new MyDisposable();
            d1.Dispose();
            d1.TestThrowIfDisposed();
        }
        

        [TestMethod]
        [Desc("Confirms that IsDisposed works correctly")]
        public void TestIsDisposed()
        {
            var d1 = new MyDisposable();
            d1.Dispose();
            Assert.IsTrue(d1.IsDisposed);
        }

        [TestMethod]
        [Desc("Confirms that fields containing references are disposed and nulled")]
        public void TestDisposalOfFieldReferences()
        {
            var d1 = DisposableDumy;
            var c1 = d1.Child;
            var children = d1.Children.ToArray();   // make a copy

            d1.Dispose();

            Assert.IsNull(d1.Child);
            Assert.IsTrue(c1.IsDisposed);
            Assert.IsNull(d1.Children);

            foreach (var c in children)
                Assert.IsTrue(c.IsDisposed);
        }


    }
}